/*
 * LiveTransformFrame.java
 *
 * Created on Aug 2, 2010, 8:38:39 PM
 */
package org.xepf.tools.livetransform;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import org.xepf.xml.adapter.XSLT;
import org.xepf.xml.saxon.XSLTImp;
import org.xepf.utils.FileUtils;

/**
 *
 */
public class LiveTransformFrame extends javax.swing.JFrame {

    /** Creates new form LiveTransformFrame */
    public LiveTransformFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jScriptEditorPane = new javax.swing.JEditorPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jFirstTransformationResultEditorPane = new javax.swing.JEditorPane();
        jLiveTransformCheckbox = new javax.swing.JCheckBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jSecondTransformationResultEditorPane = new javax.swing.JEditorPane();
        jSeparator1 = new javax.swing.JSeparator();
        jTransformButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScriptEditorPane.setText("Paste here your XTemp script");
        jScriptEditorPane.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jScriptEditorPaneKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jScriptEditorPane);

        jFirstTransformationResultEditorPane.setText("Here will be the 1st transformation result");
        jFirstTransformationResultEditorPane.setMinimumSize(new java.awt.Dimension(6, 351));
        jFirstTransformationResultEditorPane.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jFirstTransformationResultEditorPaneKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jFirstTransformationResultEditorPane);

        jLiveTransformCheckbox.setSelected(true);
        jLiveTransformCheckbox.setText("Live transform");
        jLiveTransformCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLiveTransformCheckboxActionPerformed(evt);
            }
        });

        jSecondTransformationResultEditorPane.setEditable(false);
        jSecondTransformationResultEditorPane.setText("And here the 2nd transformation result");
        jScrollPane3.setViewportView(jSecondTransformationResultEditorPane);

        jTransformButton.setText("Transform");
        jTransformButton.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTransformButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLiveTransformCheckbox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 791, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 939, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTransformButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLiveTransformCheckbox))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLiveTransformCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLiveTransformCheckboxActionPerformed
        if (jLiveTransformCheckbox.isSelected() == true) {
            jTransformButton.setEnabled(false);
        }

        if (jLiveTransformCheckbox.isSelected() == false) {
            jTransformButton.setEnabled(true);
        }
    }//GEN-LAST:event_jLiveTransformCheckboxActionPerformed

    private void jScriptEditorPaneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jScriptEditorPaneKeyTyped

        XSLT transformer = new XSLTImp();

        // extension functions need to be commented out
        String xtempTranslatorFile = "src/org/edtf/engine/xtemp/translator/xtemp_translator.xsl";
        String xtempEventsFile = "test/org/edtf/test/events/oasis/noevents.xml";
        String translator = null;
        String events = null;

        List<ExtensionFunctionDefinition> extensions = new LinkedList<ExtensionFunctionDefinition>();
//        extensions.add(new Catch());
//        extensions.add(new CallAdapter());
//        extensions.add(new Post());
        try {
            transformer.registerExtensions(extensions);
        } catch (Exception ex) {
            Logger.getLogger(LiveTransformFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            translator = FileUtils.readFile(xtempTranslatorFile, (int) FileUtils.getFileSize(xtempTranslatorFile));
            events = FileUtils.readFile(xtempEventsFile, (int) FileUtils.getFileSize(xtempEventsFile));
        } catch (Exception ex) {
            String msg = ex.getMessage();
            msg.concat(ex.toString());
            jFirstTransformationResultEditorPane.setText(msg);
            jSecondTransformationResultEditorPane.setText("");
            return;
        }
        String script = jScriptEditorPane.getText();
        try {
            transformer.initialize(translator, script);
            transformer.transform();
        } catch (Exception ex) {
            jFirstTransformationResultEditorPane.setText(ex.getLocalizedMessage());
            jSecondTransformationResultEditorPane.setText("");
            return;
        }
        jFirstTransformationResultEditorPane.setText(transformer.getTransformationResult());

        try {
            transformer.initialize(transformer.getTransformationResult(), events);
            transformer.transform();
        } catch (Exception ex) {
            jSecondTransformationResultEditorPane.setText(ex.getLocalizedMessage());
            return;
        }

        jSecondTransformationResultEditorPane.setText(transformer.getTransformationResult());
    }//GEN-LAST:event_jScriptEditorPaneKeyTyped

    private void jFirstTransformationResultEditorPaneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFirstTransformationResultEditorPaneKeyPressed
        XSLT transformer = new XSLTImp();

        String xtempEventsFile = "test/org/edtf/test/events/oasis/noevents.xml";
        String events = null;

        try {
            events = FileUtils.readFile(xtempEventsFile, (int) FileUtils.getFileSize(xtempEventsFile));
        } catch (Exception ex) {
            String msg = ex.getMessage();
            msg.concat(ex.toString());
            jSecondTransformationResultEditorPane.setText(msg);
            return;
        }
        
        String script = jFirstTransformationResultEditorPane.getText();
        try {
            transformer.initialize(script, events);
            transformer.transform();
        } catch (Exception ex) {
            jSecondTransformationResultEditorPane.setText(ex.getLocalizedMessage());
            return;
        }

        jSecondTransformationResultEditorPane.setText(transformer.getTransformationResult());
    }//GEN-LAST:event_jFirstTransformationResultEditorPaneKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LiveTransformFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jFirstTransformationResultEditorPane;
    private javax.swing.JCheckBox jLiveTransformCheckbox;
    private javax.swing.JEditorPane jScriptEditorPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JEditorPane jSecondTransformationResultEditorPane;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jTransformButton;
    // End of variables declaration//GEN-END:variables
}
